<template>
    <div class="preview-container">
        <!-- 总览区域 -->
        <div class="overview-section">
            <h2 class="total-price">总价：¥{{ totalPrice.toLocaleString() }}</h2>
            <div class="bike-preview">
                <!-- 这里可以放置自行车整体预览图 -->
                <div class="bike-image">
                    <img src="../assets/bike-preview.png" alt="自行车预览" />
                    <!-- 各部件标记点 -->
                    <div 
                        v-for="(part, index) in selectedParts" 
                        :key="index"
                        :class="['part-marker', `marker-${index}`]"
                        @click="showPartDetail(part)"
                    >
                        <span class="marker-dot"></span>
                    </div>
                </div>
            </div>
        </div>

        <!-- 组件列表 -->
        <div class="parts-list">
            <div class="list-header">
                <h3>已选配件</h3>
                <span class="part-count">{{ selectedParts.length }}个配件</span>
            </div>

            <div class="parts-grid">
                <div 
                    v-for="part in selectedParts" 
                    :key="part.id"
                    class="part-card"
                    @click="showPartDetail(part)"
                >
                    <div class="part-image">
                        <img :src="part.image" :alt="part.name">
                    </div>
                    <div class="part-info">
                        <h4 class="part-name">{{ part.name }}</h4>
                        <p class="part-brand">{{ part.brand }}</p>
                        <div class="part-price">¥{{ part.price.toLocaleString() }}</div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 零件详情弹窗 -->
        <el-dialog
            v-model="dialogVisible"
            :title="currentPart?.name"
            width="90%"
            class="part-dialog"
        >
            <div class="part-detail" v-if="currentPart">
                <img :src="currentPart.image" :alt="currentPart.name">
                <div class="detail-info">
                    <div class="detail-header">
                        <h4>{{ currentPart.brand }}</h4>
                        <div class="detail-price">¥{{ currentPart.price.toLocaleString() }}</div>
                    </div>
                    <p class="detail-desc">{{ currentPart.description }}</p>
                    <div class="specs">
                        <div class="spec-item" v-for="(spec, key) in currentPart.specifications" :key="key">
                            <span class="spec-label">{{ key }}:</span>
                            <span class="spec-value">{{ spec }}</span>
                        </div>
                    </div>
                </div>
            </div>
        </el-dialog>
    </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
// import { getCartItems } from '@/api/index.js'  
import { ElMessage } from 'element-plus'

const selectedParts = ref([])
const dialogVisible = ref(false)
const currentPart = ref(null)

// 计算总价
const totalPrice = computed(() => {
    return selectedParts.value.reduce((total, part) => total + part.price, 0)
})

// 获取购物车数据
const loadCartItems = async () => {
    try {
        const res = await getCartItems()
        selectedParts.value = res.data
    } catch (error) {
        ElMessage.error('加载数据失败')
    }
}

// 显示零件详情
const showPartDetail = (part) => {
    currentPart.value = part
    dialogVisible.value = true
}

onMounted(() => {
    loadCartItems()
})
</script>

<style lang="css" scoped>
.preview-container {
    padding: 16px;
    padding-top: calc(72px + env(safe-area-inset-top)); /* 为头部组件留出空间 */
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
    background: #f5f7fa;
    min-height: 100vh;
    box-sizing: border-box;
}

.overview-section {
    background: white;
    border-radius: 16px;
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}

.total-price {
    font-size: 24px;
    font-weight: 600;
    color: #f56c6c;
    margin-bottom: 16px;
    text-align: center;
}

.bike-preview {
    position: relative;
    aspect-ratio: 16/9;
    background: #f8f9fa;
    border-radius: 12px;
    overflow: hidden;
}

.bike-image {
    width: 100%;
    height: 100%;
    position: relative;
}

.bike-image img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.part-marker {
    position: absolute;
    width: 24px;
    height: 24px;
    cursor: pointer;
    animation: pulse 2s infinite;
}

.marker-dot {
    width: 12px;
    height: 12px;
    background: #409eff;
    border-radius: 50%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

.parts-list {
    background: white;
    border-radius: 16px;
    padding: 20px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}

.list-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}

.list-header h3 {
    font-size: 18px;
    color: #333;
}

.part-count {
    color: #666;
    font-size: 14px;
}

.parts-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
}

.part-card {
    background: #f8f9fa;
    border-radius: 12px;
    overflow: hidden;
    transition: all 0.3s ease;
}

.part-card:active {
    transform: scale(0.98);
}

.part-image {
    aspect-ratio: 1;
    overflow: hidden;
}

.part-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.part-info {
    padding: 12px;
}

.part-name {
    font-size: 14px;
    font-weight: 500;
    margin-bottom: 4px;
    color: #333;
}

.part-brand {
    font-size: 12px;
    color: #666;
    margin-bottom: 8px;
}

.part-price {
    font-size: 16px;
    color: #f56c6c;
    font-weight: 500;
}

.part-dialog {
    :deep(.el-dialog__body) {
        padding: 0;
    }
}

.part-detail {
    padding: 16px;
}

.part-detail img {
    width: 100%;
    border-radius: 8px;
    margin-bottom: 16px;
}

.detail-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}

.detail-price {
    font-size: 20px;
    color: #f56c6c;
    font-weight: 600;
}

.detail-desc {
    font-size: 14px;
    color: #666;
    line-height: 1.5;
    margin-bottom: 16px;
}

.specs {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
}

.spec-item {
    font-size: 13px;
    color: #666;
}

.spec-label {
    color: #999;
    margin-right: 4px;
}

@keyframes pulse {
    0% {
        transform: scale(1);
        opacity: 1;
    }
    50% {
        transform: scale(1.2);
        opacity: 0.7;
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}

/* 标记点位置定义 */
.marker-0 { top: 30%; left: 20%; } /* 车架 */
.marker-1 { top: 40%; left: 70%; } /* 变速器 */
.marker-2 { top: 60%; left: 30%; } /* 刹车 */
/* 根据需要添加更多标记点位置 */
</style>